<?php
/**
 * A Doctrine 1.2 Database class to work with PiePHP.
 *
 * @author     Bill Lee <bill@cachecrew.com>
 * @package    PiePHP
 * @since      Version 0.0
 * @copyright  Copyright (c) 2011, Pie Software Foundation
 * @license    http://www.piephp.com/license
 *
 * @see http://www.doctrine-project.org/projects/orm/1.2/api
 */

/**
 * Database class wrapper for Doctrine 1.2. to work with PiePHP.
 * Since Doctrine is installed separately, if its path is not found
 * by default, setDoctrinePath() can be called to set the location.
 *
 * Implements Doctrine 1.2 consistent with PiePHP model/database usage
 * semantics. For more robust Doctrine usage, you can adjust your controller to
 * use the Doctrine model in the normal Doctrine expected manner.
 * 
 * Usage example:
 *   In index.php:
 *     spl_autoload_register('__autoload'); // If you have an __autoload
 *											// If '/Doctrine' not in PiePHP root:
 * 	   DoctrineDatabase::setDoctrinePath('/path/to/Doctrine'); 
 *   In your Controller:
 *     $this->defaultModelClassName = 'DoctrineModel';
 *	   $this->model = $this->loadModel();
 *     $res = $this->model->select('* from table');
 *
 * @see DoctrineModel
 * @see http://www.doctrine-project.org/projects/orm/1.2/api
 */
class DoctrineDatabase extends Database {

	/** 
	 * Path to Doctrine 1.2 library. Assume PiePHP/Doctrine.
	 * @see setPath()
	 */
	private static $_doctrinepath=NULL;
	
	private static $_init=false;
	private function _sharedInit() {
		if (self::$_doctrinepath === NULL)
			self::$_doctrinepath = dirname(dirname(__FILE__)).'/Doctrine/';
		
		require_once self::$_doctrinepath .'Doctrine.php';
		spl_autoload_register(array('Doctrine', 'autoload'));
		self::$_init=true;
	} // _sharedInit()

	public function __construct($config, $configName='default') {
//		parent::__construct($config, $configName);
// echo __METHOD__.'#'.__LINE__.' <br/>';
		self::$_init or self::_sharedInit(); // Global init

// TODO: Add port parameter
		$doctrineConfig = $config['type'] . '://' . $config['user'] . ':' .  $config['password'] . '@'.$config['host'].'/'.$config['dbname'];
		$this->connection = Doctrine_Manager::connection($doctrineConfig, $configName)
			or $this->triggerError('Doctrine could not connect to ' . $configName . ' database.');
	} // function __construct()
	
	/**
	 * Set the path to the Doctrine library (the location of the
	 * Doctrine.php file and the Doctrine directory).
	 */
	public static function setDoctrinePath($path) {
		self::$_doctrinepath = $path;
	} // setDoctrinePath()
	
	public function query($sql) {
//echo __METHOD__.'#'.__LINE__." $sql<br/>";
		$sql = trim($sql);
		$resource = $this->connection->execute($sql)
			or $this->triggerError('error in ' .__METHOD__, $sql);
		if (strpos($sql, 'INSERT') === 0) {
			$this->insertId = $this->connection->lastInsertId();
		}
		return $resource;
	} // query()

	public function select($sql, $returnAssociativeArrays = true) {
// echo __METHOD__.'#'.__LINE__." $sql<br/>";
		$fetchType = 		$returnAssociativeArrays ? Doctrine_Core::FETCH_ASSOC : Doctrine_Core::FETCH_NUM;
		$results = $this->connection->execute('SELECT '.$sql)->fetchAll($fetchType);
		return $results;
	} // select()
	
	
	

} // class DoctrineDatabase